import { MessageManager } from './MessageManager';
import { MessageType, MessagePriority, IMessage, IBattleEventData, IUnitEventData, IUIEventData, IAudioEventData } from './MessageTypes';
import { LOG } from '../../../ConsoleLogCtrl';
/**
 * 消息工具类
 * 提供便捷的消息发送和处理方法
 */
export class MessageUtils {
    
    /**
     * 获取消息管理器实例
     */
    private static getMessageManager(): MessageManager | null {
        const manager = MessageManager.getInstance();
        if (!manager) {
            LOG.warn('MessageManager未初始化');
        }
        return manager;
    }
    
    // === 战斗相关消息 ===
    
    /**
     * 发送攻击事件消息
     * @param attackerId 攻击者ID
     * @param targetId 目标ID
     * @param damage 伤害值
     * @param isCritical 是否暴击
     * @param skillId 技能ID
     */
    public static sendAttackEvent(
        attackerId: string, 
        targetId: string, 
        damage: number, 
        isCritical: boolean = false,
        skillId?: string
    ): void {
        const data: IBattleEventData = {
            attackerId,
            targetId,
            damage,
            isCritical,
            skillId
        };
        
        this.getMessageManager()?.sendMessage(
            MessageType.ATTACK_EVENT, 
            data, 
            MessagePriority.HIGH
        );
    }
    
    /**
     * 发送伤害事件消息
     * @param targetId 目标ID
     * @param damage 伤害值
     * @param isCritical 是否暴击
     * @param isDodge 是否闪避
     * @param isBlock 是否格挡
     */
    public static sendDamageEvent(
        targetId: string, 
        damage: number, 
        isCritical: boolean = false,
        isDodge: boolean = false,
        isBlock: boolean = false
    ): void {
        const data: IBattleEventData = {
            targetId,
            damage,
            isCritical,
            isDodge,
            isBlock
        };
        
        this.getMessageManager()?.sendMessage(
            MessageType.DAMAGE_EVENT, 
            data, 
            MessagePriority.HIGH
        );
    }
    
    /**
     * 发送治疗事件消息
     * @param targetId 目标ID
     * @param healValue 治疗值
     * @param skillId 技能ID
     */
    public static sendHealEvent(
        targetId: string, 
        healValue: number, 
        skillId?: string
    ): void {
        const data: IBattleEventData = {
            targetId,
            healValue,
            skillId
        };
        
        this.getMessageManager()?.sendMessage(
            MessageType.HEAL_EVENT, 
            data, 
            MessagePriority.HIGH
        );
    }
    
    /**
     * 发送死亡事件消息
     * @param unitId 单位ID
     * @param killerId 击杀者ID
     */
    public static sendDeathEvent(unitId: string, killerId?: string): void {
        const data: IBattleEventData = {
            targetId: unitId,
            attackerId: killerId
        };
        
        this.getMessageManager()?.sendMessage(
            MessageType.DEATH_EVENT, 
            data, 
            MessagePriority.HIGH
        );
    }
    
    // === 单位相关消息 ===
    
    /**
     * 发送单位创建消息
     * @param unitId 单位ID
     * @param unitType 单位类型
     * @param position 位置
     */
    public static sendUnitCreated(
        unitId: string, 
        unitType: string, 
        position?: { x: number; y: number }
    ): void {
        const data: IUnitEventData = {
            unitId,
            unitType,
            position
        };
        
        this.getMessageManager()?.sendMessage(
            MessageType.UNIT_CREATED, 
            data, 
            MessagePriority.NORMAL
        );
    }
    
    /**
     * 发送血量变化消息
     * @param unitId 单位ID
     * @param currentHealth 当前血量
     * @param maxHealth 最大血量
     */
    public static sendHealthChanged(
        unitId: string, 
        currentHealth: number, 
        maxHealth: number
    ): void {
        const data: IUnitEventData = {
            unitId,
            currentHealth,
            maxHealth
        };
        
        this.getMessageManager()?.sendMessage(
            MessageType.HEALTH_CHANGED, 
            data, 
            MessagePriority.NORMAL
        );
    }
    
    /**
     * 发送等级提升消息
     * @param unitId 单位ID
     * @param level 新等级
     * @param experience 当前经验值
     */
    public static sendLevelUp(
        unitId: string, 
        level: number, 
        experience?: number
    ): void {
        const data: IUnitEventData = {
            unitId,
            level,
            experience
        };
        
        this.getMessageManager()?.sendMessage(
            MessageType.LEVEL_UP, 
            data, 
            MessagePriority.HIGH
        );
    }
    
    /**
     * 发送经验值获得消息
     * @param unitId 单位ID
     * @param experience 获得的经验值
     */
    public static sendExpGained(unitId: string, experience: number): void {
        const data: IUnitEventData = {
            unitId,
            experience
        };
        
        this.getMessageManager()?.sendMessage(
            MessageType.EXP_GAINED, 
            data, 
            MessagePriority.NORMAL
        );
    }
    
    // === UI相关消息 ===
    
    /**
     * 发送UI显示消息
     * @param elementId UI元素ID
     * @param uiType UI类型
     * @param params 额外参数
     */
    public static sendUIShow(
        elementId: string, 
        uiType?: string, 
        params?: any
    ): void {
        const data: IUIEventData = {
            elementId,
            uiType,
            visible: true,
            params
        };
        
        this.getMessageManager()?.sendMessage(
            MessageType.UI_SHOW, 
            data, 
            MessagePriority.NORMAL
        );
    }
    
    /**
     * 发送UI隐藏消息
     * @param elementId UI元素ID
     * @param uiType UI类型
     */
    public static sendUIHide(elementId: string, uiType?: string): void {
        const data: IUIEventData = {
            elementId,
            uiType,
            visible: false
        };
        
        this.getMessageManager()?.sendMessage(
            MessageType.UI_HIDE, 
            data, 
            MessagePriority.NORMAL
        );
    }
    
    /**
     * 发送按钮点击消息
     * @param buttonId 按钮ID
     * @param params 额外参数
     */
    public static sendButtonClicked(buttonId: string, params?: any): void {
        const data: IUIEventData = {
            elementId: buttonId,
            uiType: 'button',
            params
        };
        
        this.getMessageManager()?.sendMessage(
            MessageType.BUTTON_CLICKED, 
            data, 
            MessagePriority.NORMAL
        );
    }
    
    // === 游戏状态消息 ===
    
    /**
     * 发送游戏开始消息
     * @param gameData 游戏数据
     */
    public static sendGameStart(gameData?: any): void {
        this.getMessageManager()?.sendMessage(
            MessageType.GAME_START, 
            gameData, 
            MessagePriority.HIGH
        );
    }
    
    /**
     * 发送游戏结束消息
     * @param result 游戏结果
     */
    public static sendGameEnd(result?: any): void {
        this.getMessageManager()?.sendMessage(
            MessageType.GAME_END, 
            result, 
            MessagePriority.HIGH
        );
    }
    
    /**
     * 发送战斗开始消息
     * @param battleData 战斗数据
     */
    public static sendBattleStart(battleData?: any): void {
        this.getMessageManager()?.sendMessage(
            MessageType.BATTLE_START, 
            battleData, 
            MessagePriority.HIGH
        );
    }
    
    /**
     * 发送战斗结束消息
     * @param battleResult 战斗结果
     */
    public static sendBattleEnd(battleResult?: any): void {
        this.getMessageManager()?.sendMessage(
            MessageType.BATTLE_END, 
            battleResult, 
            MessagePriority.HIGH
        );
    }
    
    // === 系统消息 ===
    
    /**
     * 发送系统错误消息
     * @param errorCode 错误代码
     * @param message 错误信息
     * @param details 错误详情
     */
    public static sendSystemError(
        errorCode: string, 
        message: string, 
        details?: any
    ): void {
        const data = {
            errorCode,
            message,
            details
        };
        
        this.getMessageManager()?.sendMessage(
            MessageType.SYSTEM_ERROR, 
            data, 
            MessagePriority.HIGH
        );
    }
    
    /**
     * 发送系统警告消息
     * @param message 警告信息
     * @param details 详情
     */
    public static sendSystemWarning(message: string, details?: any): void {
        const data = {
            message,
            details
        };
        
        this.getMessageManager()?.sendMessage(
            MessageType.SYSTEM_WARNING, 
            data, 
            MessagePriority.NORMAL
        );
    }
    
    /**
     * 发送系统信息消息
     * @param message 信息内容
     * @param details 详情
     */
    public static sendSystemInfo(message: string, details?: any): void {
        const data = {
            message,
            details
        };
        
        this.getMessageManager()?.sendMessage(
            MessageType.SYSTEM_INFO, 
            data, 
            MessagePriority.LOW
        );
    }
    
    // === 便捷订阅方法 ===
    
    /**
     * 订阅战斗事件
     * @param callback 回调函数
     * @param target 目标对象
     */
    public static subscribeBattleEvents(
        callback: (message: IMessage<IBattleEventData>) => void, 
        target?: any
    ): void {
        const manager = this.getMessageManager();
        if (!manager) return;
        
        manager.subscribe(MessageType.ATTACK_EVENT, callback, target);
        manager.subscribe(MessageType.DAMAGE_EVENT, callback, target);
        manager.subscribe(MessageType.HEAL_EVENT, callback, target);
        manager.subscribe(MessageType.DEATH_EVENT, callback, target);
    }
    
    /**
     * 订阅单位事件
     * @param callback 回调函数
     * @param target 目标对象
     */
    public static subscribeUnitEvents(
        callback: (message: IMessage<IUnitEventData>) => void, 
        target?: any
    ): void {
        const manager = this.getMessageManager();
        if (!manager) return;
        
        manager.subscribe(MessageType.UNIT_CREATED, callback, target);
        manager.subscribe(MessageType.UNIT_DESTROYED, callback, target);
        manager.subscribe(MessageType.HEALTH_CHANGED, callback, target);
        manager.subscribe(MessageType.LEVEL_UP, callback, target);
        manager.subscribe(MessageType.EXP_GAINED, callback, target);
    }
    
    /**
     * 订阅UI事件
     * @param callback 回调函数
     * @param target 目标对象
     */
    public static subscribeUIEvents(
        callback: (message: IMessage<IUIEventData>) => void, 
        target?: any
    ): void {
        const manager = this.getMessageManager();
        if (!manager) return;
        
        manager.subscribe(MessageType.UI_SHOW, callback, target);
        manager.subscribe(MessageType.UI_HIDE, callback, target);
        manager.subscribe(MessageType.BUTTON_CLICKED, callback, target);
        manager.subscribe(MessageType.PANEL_OPENED, callback, target);
        manager.subscribe(MessageType.PANEL_CLOSED, callback, target);
    }
    
    /**
     * 取消目标对象的所有订阅
     * @param target 目标对象
     */
    public static unsubscribeAll(target: any): void {
        this.getMessageManager()?.unsubscribeTarget(target);
    }
    
    // === 调试和统计方法 ===
    
    /**
     * 获取消息系统统计信息
     */
    public static getStatistics(): any {
        return this.getMessageManager()?.getStatistics();
    }
    
    /**
     * 打印消息系统状态
     */
    public static debugPrintStatus(): void {
        const manager = this.getMessageManager();
        if (!manager) {
            LOG.log('MessageManager未初始化');
            return;
        }
        
        const stats = manager.getStatistics();
        LOG.log('=== 消息系统状态 ===');
        LOG.log('队列长度:', stats.queueLength);
        LOG.log('总订阅者数:', stats.totalSubscribers);
        LOG.log('处理间隔:', stats.processInterval + 'ms');
        LOG.log('每帧最大处理数:', stats.maxPerFrame);
        LOG.log('==================');
    }
    
    /**
     * 清空消息队列
     */
    public static clearQueue(): void {
        this.getMessageManager()?.clearMessageQueue();
    }
}